"""
==================================
业务流场景：
    组装并定义用例执行顺序
==================================
"""
import unittest
import datetime
import requests
from jacksonreport import TestRunner
from time import sleep
from config import conf


def case_seq():
    # 获取case文件夹的绝对路径
    case_dir = conf.get_case_path()
    # 先执行登录操作获取token
    case1 = unittest.defaultTestLoader.discover(case_dir, 'testCaseLogin.py')
    runner = TestRunner(case1)
    runner.run()

    sleep(3)

    # 组装需要顺序执行的测试用例到列表中
    case_all = \
        [
            unittest.defaultTestLoader.discover(case_dir, 'testCaseCreateStore.py'),
            unittest.defaultTestLoader.discover(case_dir, 'testCaseEditStore.py'),
            unittest.defaultTestLoader.discover(case_dir, 'testCaseGetMyProduct.py'),
            unittest.defaultTestLoader.discover(case_dir, 'testCaseCreateSecAgent.py')
        ]
    # for循环执行，每个用例执行间隔3s
    for i in case_all:
        runner = TestRunner(i)
        runner.rerun_run(count=3, interval=5)
        sleep(3)

    # 执行完成后推送通知到企业微信
    wehook = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=4419759d-765b-47fd-94a2-b33aff4f9156'
    headers = {"Content-Type": "application/json"}
    current_time = datetime.datetime.now().strftime("%Y-%m-%d")
    data = {
        "msgtype": "text",
        "text": {
            "content": current_time + ":接口自动化测试已完成，详情请移步终端查看测试报告",
            "mentioned_list": ["luoyulong", "@all"]
        }
    }
    requests.post(url=wehook, headers=headers, json=data)


if __name__ == '__main__':
    case_seq()
